//
//  CXTitleView.m
//  喜马拉雅FM
//
//  Created by hcios on 2017/8/27.
//  Copyright © 2017年 hcios. All rights reserved.
//

#import "CXTitleView.h"
#import "CXTitleButton.h"

@interface CXTitleView()

//三角形箭头
@property (nonatomic, strong) UIImageView * arrowImgView;

//更多
@property (nonatomic, strong) UIButton * moreButton;

@end

@implementation CXTitleView

-(instancetype)initWithTitle:(NSString *)title hasMore:(BOOL)more {
    if (self = [super init]) {
        self.title = title;
        self.arrowImgView.image = kGetImage(@"tabbar_np_play");
        [self.titleLabel setTextColor:[UIColor blackColor]];
        if (more) {
            [self moreButton];
        }
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}


-(UIImageView *)arrowImgView {
    if (!_arrowImgView) {
        _arrowImgView = [[UIImageView alloc] init];
        [self addSubview:_arrowImgView];
        [_arrowImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(8);
            make.bottom.mas_equalTo(-12);
            make.size.mas_equalTo(CGSizeMake(10, 10));
        }];
    }
    return _arrowImgView;
}


-(UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        [self addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.arrowImgView);
            make.left.mas_equalTo(self.arrowImgView.mas_right);
            make.width.mas_equalTo(150);
        }];
        _titleLabel.font = [UIFont systemFontOfSize:15];
        _titleLabel.text = _title;
    }
    return _titleLabel;
}



-(UIButton *)moreButton {
    if (!_moreButton) {
        _moreButton = [[CXTitleButton alloc] init];
        [self addSubview:_moreButton];
        [_moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-5);
            make.centerY.mas_equalTo(self.titleLabel);
            make.size.mas_equalTo(CGSizeMake(60, 20));
        }];
        [_moreButton setTitle:@"更多" forState:UIControlStateNormal];
        [_moreButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_moreButton setImage:[UIImage imageNamed:@"cell_arrow"] forState:UIControlStateNormal];
        [_moreButton cx_layoutButtonWithEdgeInsetsStyle:CX_ButtonEdgeInsetsStyleRight imageTitleSpace:10];
        
        
        //按钮点击事件
        [_moreButton addTarget:self action:@selector(didClickMoreBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _moreButton;
}


-(void)didClickMoreBtn {
    if ([self.delegate respondsToSelector:@selector(didClicktitleView:)]) {
        [self.delegate didClicktitleView:self.tag];
    }
    NSLog(@"点击了更多 %ld", self.tag);
}

@end
